<?php

namespace brand\components;

use brand\logic\BrandHelper;
use brand\logic\Config;
use brand\logic\Database;
use brand\page\Form;
use brand\tools\Functions;

class Arrays extends Component
{

    public function _init()
    {
        $this->component = 'arrays';
    }

    public function Form()
    {
        if (!is_array($this->value)) $this->value = json_decode($this->value,1);
        $this->form_column = Form::$SurfaceForm->arrays($this->field,$this->label,$this->value);

        $this->columnsCheckPublic();
        //携带的参数
        $options = $this->getFormParam('options') ?: [];
        $sub_columns = [];

        foreach ($options as $option) {

            if (!$option || !is_array($option)) continue;

            foreach ($option as $key => $item) {

                if (is_object($item)){
                    $sub_columns[] = $item;
                    continue;
                }

                $sub_method = $item['form']['method'] ?? "";
                if (!$sub_method) continue;

                $ClassName = Config::BRAND_NAMESPACE . Functions::toCamelCase($sub_method);
                if (class_exists($ClassName)){
                    $ClassObject = new $ClassName($item,['id'=>100027]);
                    $ClassObject->field = $key;
                    $ClassObject->buildColumns();
                    $sub_columns[] = $ClassObject->Form();
                }

            }
        }

        
        $props = $this->props;
        if (!isset($props['title'])) $props['title'] = false;
        if (!isset($props['span'])) $props['span'] = 24;
        if (!isset($props['append'])) $props['append'] = true;

        if($props) $this->form_column->props($props);
        if ($sub_columns) $this->form_column->options($sub_columns);

        return $this->form_column;
    }



    /**
     * 表单值字段过滤
     */
    public function checkFormValue()
    {
        if ($this->value && !is_array($this->value))  $this->value = json_decode($this->value,JSON_ERROR_DEPTH);
    }
    
    /**
     * 更新/新增提交字段过滤
     * @param $key
     * @param $value
     */
    public function checkUpdateData($key, $value)
    {
        if (is_array($value)) $value = json_encode($value);
        return $value;
    }

    /**
     * 数据表字段创建CURD
     * @param $field
     * @param $props
     */
    public function DbToCurd($field,$props = [])
    {

        //默认不生成key value，自己去创建key value
        return \brand\Brand::Builder()->form_props($props)->form_method('arrays')->table(false);

    }

    public function buildConfigColumn($tableConfigValue, $curd_method = 'curd_method'){



        $brandList = Config::getBrandList();
        if (isset($brandList['Arrays'])) unset($brandList['Arrays']);

        $brandOptions = BrandHelper::formatOptions($brandList);


        $options['arrays_method'] = \brand\Brand::Builder('类型','select')->form_options($brandOptions)->build();
        $options['value'] = \brand\Brand::Builder('值(value)')->form_marker('不支持纯数字')->build();
        $options['label'] = \brand\Brand::Builder('描述(label)')->build();

        $arraysValue = [];
        $value = [];
        $confOptions = $tableConfigValue['form']['options'][0] ?? [];
        foreach ($confOptions as $key =>$confOption) {
            $method = $confOption['form']['method'] ?? "";
            if(!$method) continue;
            $value[] = [
                'arrays_method' =>  $method,
                'value' =>  $key,
                'label' =>  $confOption['title'] ?? "",
            ];
            $arraysValue[$method] = $confOption;
        }

        $options_value = [];
        foreach ($brandList as $brandMethod => $brandLabel) {
            if ($brandMethod == 'Arrays') continue;

            $ClassName = Config::BRAND_NAMESPACE . Functions::toCamelCase($brandMethod);

            if (class_exists($ClassName)){

                $ClassObject = new $ClassName();
                $_values = $arraysValue[$brandMethod] ?? [];
                $methodConfig = $ClassObject->buildConfigColumn($_values,'arrays_method');
                $options[$brandMethod] = $methodConfig['column'];
                $options_value[$brandMethod] = $methodConfig['value'];

            }

        }


        $cloumn = \brand\Brand::Builder('选项配置','arrays')
            ->form_options([$options])
            ->form_props([
                'append'    =>  true,
                'title' => false
            ])->form_visible([['prop' => $curd_method, 'value' => 'Arrays']])->build();



        foreach ($value as $key => $item) {
            foreach ($options_value as $k => $ov) {
                $item[$k] = $ov;
            }
            $value[$key] = $item;
        }



        return ['column' => $cloumn,'value' =>$value];

    }


    public function methodFormConf($form,$table,$params)
    {
        $options = [];
        foreach ($params['param'] as $param) {
            if (!isset($param['arrays_method'])) continue;
            $sub_form = [];
            $sub_form['method'] = $param['arrays_method'];
            $sub_form['validate'] = [ ['required' => $param['validate_required'] ?? 0] ];
            $sub_table = [];
            $ClassName = Config::BRAND_NAMESPACE . Functions::toCamelCase($sub_form['method']);
            $ClassObject = new $ClassName();
            $sub_params['param'] = $param[$param['arrays_method']] ?? 'input';;

            [$sub_form,$sub_table] = $ClassObject->methodFormConf($sub_form,$sub_table,$sub_params);
            $options[$param['value']] = [
                'form'  =>  $sub_form,
                'table'  =>  $sub_table,
                'title' =>  $param['label']
            ];
        }

        $form['options'][] = $options;
        $form['table'] = false;

        return [$form,$table];
    }







}